package com.spaceobj.chess.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spaceobj.chess.common.Result;
import com.spaceobj.chess.entity.MatchQueue;
import com.spaceobj.chess.service.MatchQueueService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "匹配队列管理", description = "匹配队列相关接口")
@RestController
@RequestMapping("/api/match-queue")
@RequiredArgsConstructor
public class MatchQueueController {

    private final MatchQueueService matchQueueService;

    @Operation(summary = "加入匹配队列")
    @PostMapping
    public Result<MatchQueue> joinQueue(@RequestBody MatchQueue matchQueue) {
        matchQueueService.save(matchQueue);
        return Result.success(matchQueue);
    }

    @Operation(summary = "更新匹配队列状态")
    @PutMapping("/{id}")
    public Result<MatchQueue> updateQueueStatus(
            @Parameter(description = "队列ID") @PathVariable Integer id,
            @RequestBody MatchQueue matchQueue) {
        matchQueue.setUserId(id);
        matchQueueService.updateById(matchQueue);
        return Result.success(matchQueue);
    }

    @Operation(summary = "退出匹配队列")
    @DeleteMapping("/{id}")
    public Result<Void> leaveQueue(@Parameter(description = "队列ID") @PathVariable Integer id) {
        matchQueueService.removeById(id);
        return Result.success();
    }

    @Operation(summary = "获取队列详情")
    @GetMapping("/{id}")
    public Result<MatchQueue> getQueueInfo(@Parameter(description = "队列ID") @PathVariable Integer id) {
        return Result.success(matchQueueService.getById(id));
    }

    @Operation(summary = "分页查询匹配队列")
    @GetMapping
    public Result<Page<MatchQueue>> listQueues(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "用户ID") @RequestParam(required = false) Integer userId) {
        Page<MatchQueue> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<MatchQueue> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(MatchQueue::getUserId, userId);
        }
        return Result.success(matchQueueService.page(pageParam, queryWrapper));
    }
} 